package com.woniu.market_stall.web;

import com.github.pagehelper.PageInfo;
import com.woniu.market_stall.entity.Order;
import com.woniu.market_stall.result.ResultResponse;
import com.woniu.market_stall.service.AuctionOrderService;
import com.woniu.market_stall.service.RentOrderService;
import com.woniu.market_stall.service.StallService;
import com.woniu.market_stall.vo.AuctionOrderVo;
import com.woniu.market_stall.vo.RentOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.UUID;

@RestController
@RequestMapping("auctionOrder")
public class AuctionOrderController {
    @Autowired
    AuctionOrderService auctionOrderService;
    @Value("${contractPath}")
    private String contractPath;
    @Autowired
    StallService stallService;

    @GetMapping("/list")
    public ResultResponse<PageInfo<AuctionOrderVo>> list(@RequestParam(value = "now",defaultValue = "1") Integer now,
                                                         @RequestParam(value = "size",defaultValue = "3")Integer size,
                                                         @RequestParam(value = "orderId") String orderId, @RequestParam(value = "statusPay",defaultValue = "2")Integer statusPay){
        ResultResponse<PageInfo<AuctionOrderVo>> result = null;
        try {
            PageInfo<AuctionOrderVo> pageInfo = auctionOrderService.queryByPage(now, size, orderId,statusPay);
            result = new ResultResponse<>(200,"查询竞拍订单成功!",pageInfo);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询竞拍订单失败!");
        }

        return result ;
    }
    @GetMapping("/queryAuctionOrderByOrderId/{orderId}")
    public ResultResponse<Order> queryAuctionOrderByOrderId(@PathVariable("orderId") Long orderId){
        ResultResponse<Order> result = null;
        try {
            Order order=auctionOrderService.queryAuctionOrderByOrderId(orderId);
            result = new ResultResponse<>(200,"查询成功!",order);
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"查询失败!");
        }
        return result ;
    }
    @DeleteMapping("/removeOrderById/{orderId}")
    public ResultResponse<Void> removeOrderById(@PathVariable("orderId") String orderId){
        ResultResponse<Void> result = null;
        try {
            auctionOrderService.removeOrderById(orderId);
            result = new ResultResponse<>(200,"删除竞拍订单成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"删除竞拍订单失败!");
        }
        return result ;
    }
    @PutMapping("/modify")
    public ResultResponse<Void> modify(@RequestBody Order order){
        ResultResponse<Void> result = null;

        try {
            //根据 ID 查询出来 当前数据  把前台传递过来的数据  赋值到新查询出来的对象上
            auctionOrderService.modifyAuctionOrderByOrderId(order);
            result = new ResultResponse<>(200,"修改竞拍订单成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"修改竞拍订单失败!");
        }
        return result ;
    }
    @PostMapping("/add")
    public ResultResponse<Void> add(@RequestBody Order order){
        ResultResponse<Void> result = null;

        try {
            order.setOrderId(System.currentTimeMillis()+"");
            order.setType(1);
            order.setInsertTime(new Date());
            order.setIsDelete(0);
            int marketId = stallService.queryMarketIdByStallId(order.getStallId());
            order.setMarketId(marketId);
            auctionOrderService.add(order);
            result = new ResultResponse<>(200,"添加订单成功!");
        }catch (RuntimeException e){
            e.printStackTrace();
            result = new ResultResponse<>(201,"添加订单失败!");
        }
        return result ;
    }
    @PostMapping("/uploadContract")
    public ResultResponse<String> uploadContract(
            @RequestParam(value = "uploadContract") MultipartFile multipartFile) {
        System.out.println("multipartFile"+multipartFile);
        ResultResponse<String> result = null;
        try {
            //获取 上传文件的名字
            String filename = multipartFile.getOriginalFilename();
            //重新定义上传文件名字
            filename = "合同"+ UUID.randomUUID().toString().replace("-", "").substring(0,15) + filename.substring(filename.lastIndexOf("."));

            File f = new File(contractPath);
            if (f.exists()) {
                f.mkdirs();
            }

            File file = new File(f, filename);
            //上传
            multipartFile.transferTo(file);
            result = new ResultResponse<>(200, "上传成功", filename);
        } catch (Exception e) {
            e.printStackTrace();
            result = new ResultResponse<>(201, "上传失败");
        }
        return result;
    }
}
